package cn.itsource.ymcc.consumer;

import cn.itsource.ymcc.constant.Constants;
import cn.itsource.ymcc.constant.CourseConstants;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Slf4j
@Component
@RocketMQMessageListener(
        consumerGroup = "CourseEMAILConsumer",
        topic = CourseConstants.TOPIC_COURSE_EMAIL_PUSH,
        selectorExpression = CourseConstants.TAGS_COURSE_EMAIL_PUSH
)
public class CourseEMAILConsumer implements RocketMQListener<MessageExt> {

    @Override
    public void onMessage(MessageExt messageExt) {
        try {
            byte[] body = messageExt.getBody();
            String message = new String(body, Constants.UTF8);
            List<Long> courseIds = JSON.parseArray(message, Long.class);
            for (Long courseId : courseIds) {
                //根据课程id去远程调用course 找用户id
                //在去远程调用去找用户信息 才能得到邮箱
                //调用邮箱的接口
                System.out.println("EMAIL：您关注的xxx课程已经上线，请到xxx地方去观看 http://sdfsdfsaddsafds/"+courseId);
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
